/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import br.ufrn.engcomp.pds.helloopc.algoritmos.AbstractAlgoritmoControlador;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Felipe Rivas
 */
public class PID extends AbstractAlgoritmoControlador {

    private static final String SENSOR = "sensor";
    private static final String CONTROLE = "controle";
    //private static final String REFERENCIA = "ref";
    private static final String KP = "Kp";
    private static final String KI = "Ki";
    private static final String KD = "Kd";

    @Override
    public Map<String, Object> calcularResposta(Map<String, Object> valoresProcesso, double referencia, double tempo) {
        Map<String, Object> valoresManipuladas = new HashMap<String, Object>();

        double valor = Double.parseDouble(valoresProcesso.get(SENSOR).toString());
        //System.out.println("VALOR: "+valor);
        
        //double ref = this.getParametro(REFERENCIA);
        double Kp  = this.getParametro(KP);
        double Ki  = this.getParametro(KI);
        double Kd  = this.getParametro(KD);

        double e = referencia - valor;

        double P = Kp * e;
        double I = erroAtual + Ki * e;
        double D = Kd * (erroAtual - e) / tempo;

        erroAtual = e;
        //System.out.println("ERROOOOOOOOOOOOOOOOOOOOO: "+erroAtual);

        valoresManipuladas.put(CONTROLE, P + I + D);

        return valoresManipuladas;
    }
    
}
